package com.zjw.zy.coreui.fragment.holder

import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.zjw.zy.R
import com.zjw.zy.base.BaseViewHolder
import com.zjw.zy.bean.WsMessage
import com.zjw.zy.constant.WechatMessageSubInfo
import com.zjw.zy.ext.endToEndOf
import com.zjw.zy.ext.isSender
import com.zjw.zy.ext.layoutGravity
import com.zjw.zy.ext.startToStartOf
import com.zjw.zy.utils.AppUtils
import com.zjw.zy.utils.PathUtils
import com.zjw.zy.utils.ResourcesUtils
import com.zjw.zy.utils.TimeUtils
import java.util.*

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/12/4 22:28
 * @description：
 */
class WechatMessageHolder(item: View) : BaseViewHolder<WsMessage>(item) {

    companion object {
        const val EVENT_CLICK_IMAGE = "EVENT_CLICK_IMAGE"
    }

    private val face: ImageView by lazy { findViewById(R.id.wechat_face_image) }
    private val faceWrap: FrameLayout by lazy { findViewById(R.id.wechat_face_wrap) }
    private val messageWrap: FrameLayout by lazy { findViewById(R.id.wechat_message_wrap) }
    private val textMessage: TextView by lazy { findViewById(R.id.wechat_text_message) }
    private val imageMessage: ImageView by lazy { findViewById(R.id.wechat_image_message) }
    private val time: TextView by lazy { findViewById(R.id.wechat_message_time) }

    override fun onBindListener() {
        imageMessage.setOnClickListener {
            mListener?.onCallbackOtherEvent(EVENT_CLICK_IMAGE, data.text/*path*/)
        }
    }

    override fun onBindData(data: WsMessage?) {
        data?.let {

            val subInfo = data.info.substring(WsMessage.WECHAT_MESSAGE_INFO.length)

            if (subInfo == WechatMessageSubInfo.TIME) {
                //仅仅标记时间
                onFlagTime(data)
                return
            }

            if (subInfo == WechatMessageSubInfo.TEXT) {
                //这里可以通过Span注入后面的表情包需求
                textMessage.visibility = View.VISIBLE
                imageMessage.visibility = View.GONE
                textMessage.text = data.text
            }

            if (subInfo == WechatMessageSubInfo.IMAGE) {
                textMessage.visibility = View.GONE
                imageMessage.visibility = View.VISIBLE
                imageMessage.setImageBitmap(PathUtils.getBitmapFromPath(data.text))
            }

            if (it.isSender()) {
                onBindSelf(data)
            } else {
                onBindLover(data)
            }
        }
    }

    private fun onFlagTime(data: WsMessage) {
        faceWrap.visibility = View.GONE
        messageWrap.visibility = View.GONE
        time.visibility = View.VISIBLE
        if (TimeUtils.isToday(data.date)) {
            time.text = TimeUtils.formatDate(Date(data.date), "HH:mm:ss")
            return
        }

        if (TimeUtils.isLastDay(data.date)) {
            time.text = ResourcesUtils.getString(
                R.string.wechat_item_time_last_day,
                TimeUtils.formatDate(Date(data.date), "HH:mm:ss")
            )
            return
        }

        time.text = TimeUtils.formatDate(Date(data.date), "yyyy/MM/dd HH:mm:ss")
    }

    private fun onBindLover(data: WsMessage) {
        time.visibility = View.GONE
        messageWrap.visibility = View.VISIBLE
        faceWrap.visibility = View.VISIBLE
        textMessage.setBackgroundResource(R.drawable.wechat_you_color)
        textMessage.layoutGravity(Gravity.START)
        imageMessage.layoutGravity(Gravity.END)
        faceWrap.endToEndOf(ConstraintLayout.LayoutParams.UNSET, false)
        faceWrap.startToStartOf()
    }

    private fun onBindSelf(data: WsMessage) {
        time.visibility = View.GONE
        messageWrap.visibility = View.VISIBLE
        faceWrap.visibility = View.VISIBLE
        textMessage.setBackgroundResource(R.drawable.wechat_me_color)
        textMessage.layoutGravity(Gravity.END)
        imageMessage.layoutGravity(Gravity.START)
        faceWrap.startToStartOf(ConstraintLayout.LayoutParams.UNSET, false)
        faceWrap.endToEndOf()
    }
}